// 配置store
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import { composeWithDevTools } from 'redux-devtools-extension'
import reducer from '@/store/reducers/index'
import { getLocalHistories, getTokenInfo } from '@/utils/storage'
import { ThunkAction } from 'redux-thunk'
import { HomeAction } from './reducers/home'
import { ProfileAction } from './reducers/profile'
import { LoginAction } from './reducers/login'
import { SearchAction } from './reducers/search'
import { ArticleAction } from './reducers/article'
// store的第二个参数是设置初始值
const store = createStore(
  reducer,
  {
    login: getTokenInfo(),
    search: {
      suggestion: [],
      results: [],
      histories: getLocalHistories(),
    },
  },
  composeWithDevTools(applyMiddleware(thunk))
)
type RootAction = HomeAction | ProfileAction | LoginAction | SearchAction | ArticleAction
// 指定state的类型，用store.getState()可以获取到所有的数据，ReturnType可以拿到返回的值
export type RootType = ReturnType<typeof store.getState>
// 处理redux-thunk的两个参数的类型指定
// 这个里面有四个泛型
// 第一个是 thunk的action的返回类型 Promise<void>
// 第二个是 getState的返回类型
// 第三个是自己额外添加的参数
// 第四个是需要指定的action的类型
export type RootThunkAction = ThunkAction<Promise<void>, RootType, unknown, RootAction>
export default store
// store.dispatch()
